<template>
  <!--帮我写一个页面，在上面有新增按钮，下方是一个表格，操作包含修改和删除，新增会出来一个dialog，里面通过时间选择器选择开始时间和结束时间，点击修改也是出来dialog选择开始和结束时间-->
  <div>
    <el-button type="primary" @click="addReservation">新增</el-button>
    <el-table
        :data="tableData"
        border
        style="width: 100%;margin-top: 1rem">
      <el-table-column
          prop="id"
          type="index"
          label="序号"
          width="80">
      </el-table-column>
      <el-table-column
          prop="time"
          label="时间段"
          width="400">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
        title="新增预约时间"
        :visible.sync="adddialogVisible"
    >
      <el-time-picker
          v-model="addstartTime"
          placeholder="开始时间">
      </el-time-picker>
      <el-time-picker
          v-model="addendTime"
          placeholder="结束时间"
          style="margin-left: 2rem">
      </el-time-picker>
      <span slot="footer" class="dialog-footer">
    <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveReservationTime">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
        title="修改预约时间"
        :visible.sync="updatedialogVisible">
      <el-time-picker
          v-model="updatestartTime"
          placeholder="开始时间">
      </el-time-picker>
      <el-time-picker
          v-model="updateendTime"
          placeholder="结束时间" style="margin-left: 2rem">
      </el-time-picker>
      <span slot="footer" class="dialog-footer">
      <el-button @click="updatedialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="updateReservationTime">确 定</el-button>
      </span></el-dialog>
  </div>

</template>
<script>
import {getAllTime, addReservation, deleteReservation, updateReservation} from '@/utils/apis/order'

export default {
  name: "ReservationList",
  data() {
    return {
      tableData: [],
      adddialogVisible: false,
      updatedialogVisible: false,
      addstartTime: '',
      addendTime: '',
      updatestartTime: '',
      updateendTime: '',
      updateUuid: ''

    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    initData() {
      getAllTime().then(res => {
        console.log(res)
        this.tableData = res.data.data
      })
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.updatestartTime = row.time.slice(0, 8)
      this.updateendTime = row.time.slice(11, 19)
      console.log(this.updatestartTime, this.updateendTime)
      this.updatedialogVisible = true
      this.updateUuid = row.uuid
//       this.updateendTime = row.endTime
    },
    handleDelete(index, row) {
      console.log(index, row);
      const uuid = row.uuid
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteReservation(uuid).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.initData()
          }
        })

      });

    },
    addReservation() {
      this.adddialogVisible = true
    },
    saveReservationTime() {
      const param = {
        startTime: this.addstartTime,
        endTime: this.addendTime
      }
      addReservation(param).then(res => {
        console.log(res)
        if (res.data.code === 200) {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.adddialogVisible = false
          this.initData()
        }
      })
    },
    updateReservationTime() {
      const param = {
        uuid: this.updateUuid,
        startTime: this.updatestartTime,
        endTime: this.updateendTime
      }
      this.updatedialogVisible = true
      updateReservation(param).then(res => {
        console.log(res)
        if (res.data.code === 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.updatedialogVisible = false
          this.initData()
        }
      })
    }
  }
}
</script>


<style scoped>

</style>